package org.cwnuit.train.acton;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cwnuit.train.domain.Trains;
import org.cwnuit.train.domain.Wtype;
import org.cwnuit.train.service.WtypeService;
import org.cwnuit.train.utils.MapUtil;
import org.cwnuit.train.utils.PageModel;
import org.cwnuit.train.utils.Today;

@SuppressWarnings("all")
public class WtypeAction extends HttpServlet{
	
	private WtypeService wtypeService = new WtypeService();
	
	protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		String cmd = request.getParameter("cmd");
        if (cmd.equals("list")) {
        	request.setAttribute("content_tittle", "职名学习");
        	request.setAttribute("pm",listWtype(request));
			request.getRequestDispatcher("web/wtype/list.jsp").forward(request, response);
		}else if (cmd.equals("list_main")) {
        	request.setAttribute("content_tittle", "职位管理");
        	request.setAttribute("pm",listWtype(request));
			request.getRequestDispatcher("web/wtype/list_main.jsp").forward(request, response);
		}else if (cmd.equals("adding")) {
        	request.setAttribute("content_tittle", "职位添加");
			request.getRequestDispatcher("web/wtype/input.jsp").forward(request, response);
		}else if (cmd.equals("input")) {
        	request.setAttribute("content_tittle", "职位添加");
			if (request.getParameter("wid") == null||request.getParameter("wid").equals("")) {
				if (addWtype(request)) {
					request.setAttribute("msg", "恭喜你，添加成功！");
					request.getRequestDispatcher("wtype?cmd=list_main").forward(
							request, response);
				}else{
					request.setAttribute("msg", "对不起！该职位名称已经存在，信息添加失败！");
					request.getRequestDispatcher("web/wtype/input.jsp").forward(request, response);
				}
				
			} else {
				if (updateWtype(request)) {
					request.setAttribute("msg", "恭喜你，成功修改职名！");
				}else {
					request.setAttribute("msg", "对不起，职名修改未成功！");
				}
				request.getRequestDispatcher("wtype?cmd=list_main").forward(request, response);
			}
		} else if(cmd.equals("delete")){
			if(deleteWtype(request)){
				request.setAttribute("msg", "恭喜你，你已经成功删除了一条记录！");
			} else {
				request.setAttribute("msg", "对不起，你删除记录失败！");
			}
			request.getRequestDispatcher("wtype?cmd=list_main").forward(request, response);
		} else if(cmd.equals("updating")){
			int wid = Integer.parseInt(request.getParameter("wid"));
			request.setAttribute("wtype", findWtype(wid));
			request.setAttribute("content_tittle", "修改职名信息");
			request.getRequestDispatcher("web/wtype/input.jsp").forward(request, response);
		}
		
	}
	
//////////////////////////////////////////////////工具方法////////////////////////////////////////////////	
	private PageModel listWtype(HttpServletRequest request) {
		int offset = 0;
		try {
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		} catch (NumberFormatException e) {
			offset = 0;
		}
		String SQL = "SELECT * FROM Wtype ORDER BY wid DESC";
		PageModel pm = wtypeService.findByPage(offset, 10, SQL);
		return pm;
	}
	
	private boolean addWtype(HttpServletRequest request) {
		String wname = request.getParameter("wname");
		String wremark = request.getParameter("wremark");
		String SQL = "INSERT INTO Wtype(wname,wremark) VALUES('"
				+ wname + "','"
				+ wremark + "')";
		return wtypeService.addWtype(SQL);
	}

	private boolean updateWtype(HttpServletRequest request) {
		int wid = Integer.parseInt(request.getParameter("wid"));
		String wname = request.getParameter("wname");
		String wremark = request.getParameter("wremark");
		String SQL = "UPDATE Wtype SET wname = '" + wname + "',wremark = '"
				+ wremark + "' WHERE wid = " +wid;
		return wtypeService.updateWtype(SQL);
	}
	
	private boolean deleteWtype(HttpServletRequest request) {
		int wid = Integer.parseInt(request.getParameter("wid"));
		String SQL = "DELETE FROM Wtype WHERE wid = " + wid;
		return wtypeService.deleteWtype(SQL);
	}
	
	private Wtype findWtype(int wid) {
		String SQL = "SELECT * FROM Wtype WHERE wid = " + wid;
		return wtypeService.findWtype(SQL);
	}
	
}
